package com.linpq.road.controller;

import java.util.List;

import com.linpq.common.exception.BusinessException;
import com.linpq.common.utils.StringUtils;
import com.linpq.road.domain.RdRoadSection;
import com.linpq.road.service.RdRoadSectionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.linpq.common.annotation.Log;
import com.linpq.common.enums.BusinessType;
import com.linpq.road.domain.RdRoadStake;
import com.linpq.road.service.RdRoadStakeService;
import com.linpq.common.core.controller.BaseController;
import com.linpq.common.core.domain.AjaxResult;
import com.linpq.common.utils.poi.ExcelUtil;
import com.linpq.common.core.page.TableDataInfo;

/**
 * 路桩Controller
 *
 * @author linpq
 * @date 2021-08-03
 */
@Controller
@RequestMapping("/road/road_stake")
public class RdRoadStakeController extends BaseController {
    private String prefix = "road/road_stake";

    @Autowired
    private RdRoadStakeService rdRoadStakeService;
    @Autowired
    private RdRoadSectionService rdRoadSectionService;

    @RequiresPermissions("road:road_stake:view")
    @GetMapping()
    public String road_stake() {
        return prefix + "/road_stake";
    }


    /**
     * 表单
     */
    @GetMapping("/form")
    public String form() {
        return prefix + "/stake_form";
    }

    /**
     * 查询路桩列表
     */
    @RequiresPermissions("road:road_stake:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RdRoadStake rdRoadStake) {
        startPage();
        List<RdRoadStake> list = rdRoadStakeService.selectRdRoadStakeList(rdRoadStake);
        return getDataTable(list);
    }

    /**
     * 导出路桩列表
     */
    @RequiresPermissions("road:road_stake:export")
    @Log(title = "路桩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RdRoadStake rdRoadStake) {
        List<RdRoadStake> list = rdRoadStakeService.selectRdRoadStakeList(rdRoadStake);
        ExcelUtil<RdRoadStake> util = new ExcelUtil<RdRoadStake>(RdRoadStake. class);
        return util.exportExcel(list, "road_stake");
    }

    /**
     * 新增路桩
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存路桩
     */
    @RequiresPermissions("road:road_stake:add")
    @Log(title = "路桩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RdRoadStake rdRoadStake) {
        return toAjax(rdRoadStakeService.insertRdRoadStake(rdRoadStake));
    }

    /**
     * 修改路桩
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        RdRoadStake rdRoadStake =rdRoadStakeService.selectRdRoadStakeById(id);
        mmap.put("rdRoadStake", rdRoadStake);
        return prefix + "/edit";
    }

    /**
     * 修改保存路桩
     */
    @RequiresPermissions("road:road_stake:edit")
    @Log(title = "路桩", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RdRoadStake rdRoadStake) {
        return toAjax(rdRoadStakeService.updateRdRoadStake(rdRoadStake));
    }

    /**
     * 删除路桩
     */
    @RequiresPermissions("road:road_stake:remove")
    @Log(title = "路桩", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rdRoadStakeService.deleteRdRoadStakeByIds(ids));
    }

    /**
     * 初始化路桩
     * @param id
     * @return
     */
    @RequiresPermissions("road:road_stake:insertByRoadStake")
    @RequestMapping(value = "/insertByRoadStake/{sectionid}",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult insertByRoadStatke(@PathVariable(value = "sectionid") String id){
        if (StringUtils.isNull(id)&&"".equals(id)){
            throw new BusinessException("路段编号异常");
        }
        AjaxResult ajaxResult = null;
        try {
            ajaxResult = rdRoadStakeService.insertByRoadStake(id);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * 路桩的所有详细信息
     * @param
     * @return
     */

    @RequiresPermissions("road:road_stake:rdRoadStakeList")
    @PostMapping("/rdRoadStakeList")
    @ResponseBody
    public TableDataInfo rdRoadStakeList() {
        startPage();
        List<RdRoadStake> list = rdRoadStakeService.selectRdRoadStakeList();
        return getDataTable(list);
    }

}
